import { Component, OnInit, Output, Input, TemplateRef, DoCheck, OnChanges, EventEmitter, ContentChild, SimpleChanges } from '@angular/core';
@Component({
    templateUrl: './table.component.html',
    selector: 'zs-table'
})
export class TableComponent implements OnInit{
    constructor() { };
    public _data: Array<any>;
    @ContentChild('tmpl') tmpl: TemplateRef<any>;
    @ContentChild('thead') thead: TemplateRef<any>;
    @Input() selection;
    @Output() selectionChange: EventEmitter<any> = new EventEmitter<any>();
    @Input() set data(value: Array<any>) {
      this._data = this.deepClone(value) as Array<any>;
      this._data.forEach((item, index) => {
        item.checked = item.checked === void 0 ? false : !!item.checked
      })
    };
    @Input() tableBodyStyle = ''
    @Input() height = null
    @Input() tableHeaderClass:string
    ngOnInit() {
    }
    selectAll() {
      let isSelectAll = this._data.every(item => item.checked);
      this._data.forEach(item => item.checked = !isSelectAll);
      setTimeout(()=>this.throwSelection(),10) 
    }
  
    isSelectAll(): boolean {
      let result = this._data.every(item => item.checked)&&!!this._data.length
      return result
    }
    selectThis(){
      setTimeout(()=>this.throwSelection(),10) 
    }
    clearSelection() {
      this._data.forEach(item => item.checked = false)
      setTimeout(()=>this.throwSelection(),10) 
    }
    throwSelection() {
      let result = this._data.filter(item => item.checked)
      this.selectionChange.emit(result);
      return result;
    }
    // ngDoCheck() {
    //   setTimeout(()=>this.throwSelection(),10) 
    // }
  
    
    private deepClone(obj: any) {
      return JSON.parse(JSON.stringify(obj));
    }
    
}